<template>
  <div class="upload__file__container">
      <div class="file__container">
        <el-upload
          class="avatar-uploader"
          :action="actionData"
          accept=".JPG,.PNG,.JPEG"
          :show-file-list="false"
          :headers="headersToken"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <p>请选择像素大小大于50*50的照片</p>
      </div>
  </div>
</template>
<script>
  import axios from 'axios'
  export default {
    name: 'uploadFileByTour',
    data() {
      return {
        propsFile:{},
        headersToken: {
          token: ''
        },
        imageUrl: '',
        actionData: `${axios.defaults.baseURL}/api/file-service/v1/file/uploadFileByTour`,
      }
    },
    methods: {
      handleAvatarSuccess(res, file) {
        if (res.code === 1) {
          this.$message.success(res.message);
          this.propsFile = res.result
          this.$emit('handleAvatarSuccess', res, file)
          return
        }
        this.$message.error(res.message);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || 'image/PNG';
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG,PNG 格式!');
        }
        return isJPG;
      },
    },
    mounted() {
      let { token } = JSON.parse(localStorage.getItem('logininfo'));
      this.headersToken.token = token;
    }
  }
</script>
<style lang="scss" scoped>
  .upload__file__container{
    margin-left: 30px;
    .file__container{
      width: 300px;
      height: 330px;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      p{
        text-align: center;
        width: 300px;
        height: 30px;
        line-height: 30px;
        font-size: 12px;
      }
    }
  }

</style>
<style lang="scss">
  .upload__file__container{
    .avatar-uploader{
      width: 100%;
      height: 100%;
      padding: 10px;
      overflow: hidden;
      border-radius: 6px;
      background: #EEEEEE;
      .el-upload{
        border: 1px dashed #d9d9d9;
        display: block;
        width: 100%;
        height: 100%;
        line-height: 300px;
        color: #8c939d;
        i{
          font-size: 40px;
        }
      }
    }
  }
</style>
